<script setup>
import { ref, onMounted, onUpdated } from 'vue'
import { onBeforeRouteUpdate } from 'vue-router';
import { getCategoryAPI } from '@/apis/CategoryApi';
import { useRoute } from 'vue-router';
import GoodsItem from '../Home/components/GoodItem.vue'

const category = ref([{}])
const route = useRoute()

const getCategory = async (id = route.params.id) => {
  const res = await getCategoryAPI(id)
  category.value = res.data
}

onMounted(() => {
  getCategory()
})

const bannerPir = ref([])
import { getBannerAPI } from '@/apis/CategoryApi';

onBeforeRouteUpdate((to) => {
  console.log("路由变化了");
  getCategory(to.params.id)
})
</script>

<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <div class="bread-container">
        <el-breadcrumb separator=">">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>{{category[0].categoryName}}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <!-- 分类 商品列表区域 -->
      <div class="sub-list">
        <h3>{{category[0].categoryName}}</h3>
        <ul>
          <li v-for="i in category" :key="i.id">
            <RouterLink :to="`/category/sub/${i.id}`">
              <img :src="i.image" />
              <p>{{ i.name }}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
$primary-color: #2c3e50;
$accent-color: #3498db;
$background-color: #f4f7fa;
$card-bg: #ffffff;
$border-radius: 12px;
$shadow: 0 4px 20px rgba(0, 0, 0, 0.08);

.top-category {
  background: linear-gradient(145deg, $background-color, #e8edf3);
  padding: 20px 0;

  .container {
    max-width: 1200px;
    margin: 0 auto;
  }

  h3 {
    font-size: 32px;
    font-weight: 700;
    color: $primary-color;
    text-align: left;
    margin-bottom: 32px;
    letter-spacing: -0.5px;
    position: relative;
    padding-left: 16px;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 24px;
      background: $accent-color;
      border-radius: 2px;
    }
  }

  .bread-container {
    padding: 10px 0;
    background: $card-bg;
    border-radius: $border-radius;
    box-shadow: $shadow;
    margin-bottom: 24px;

    .el-breadcrumb {
      padding: 0 24px;
      font-size: 14px;

      :deep(.el-breadcrumb__inner) {
        color: $primary-color;
        font-weight: 500;
      }

      :deep(.el-breadcrumb__inner:hover) {
        color: $accent-color;
      }

      :deep(.el-breadcrumb__separator) {
        color: #6b7280;
        margin: 0 8px;
      }
    }
  }

  .sub-list {
    background: $card-bg;
    border-radius: $border-radius;
    box-shadow: $shadow;
    padding: 20px;
    margin-bottom: 10px;

    ul {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
      gap: 24px;
      padding: 0;

      li {
        display: flex;
        justify-content: center;
        align-items: center;
        transition: transform 0.3s ease;

        a {
          display: flex;
          flex-direction: column;
          align-items: center;
          text-align: center;
          text-decoration: none;
          width: 100%;
          padding: 16px;
          border-radius: 8px;
          background: #f8fafc;
          transition: all 0.3s ease;

          &:hover {
            background: linear-gradient(90deg, #3498db, #2980b9);
            color: white;
            transform: translateY(-4px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

            p {
              color: white;
            }
          }

          img {
            width: 100px;
            height: 100px;
            object-fit: cover;
            border-radius: 8px;
            margin-bottom: 12px;
            border: 1px solid #e8ecef;
            transition: transform 0.3s ease;
          }

          p {
            font-size: 14px;
            font-weight: 500;
            color: $primary-color;
            margin: 0;
            line-height: 1.5;
          }
        }

        &:hover {
          transform: translateY(-2px);
        }
      }
    }
  }
}
</style>